import { generateKeyPairSync } from "crypto";
import fs from "fs";
import path from "path";

// 目标目录
const keysDir = path.resolve(__dirname, "keys");
if (!fs.existsSync(keysDir)) {
  fs.mkdirSync(keysDir);
}

// 生成密钥对
const { publicKey, privateKey } = generateKeyPairSync("rsa", {
  modulusLength: 2048,
  publicKeyEncoding: { type: "pkcs1", format: "pem" },
  privateKeyEncoding: { type: "pkcs1", format: "pem" },
});

// 写入文件
fs.writeFileSync(path.join(keysDir, "rsa_private_key.pem"), privateKey);
fs.writeFileSync(path.join(keysDir, "rsa_public_key.pem"), publicKey);

console.log("RSA 密钥对已生成在 keys 目录下");
